<!--
 * @页面名称:移动端登录页
 * @描述: 
 * @作者: 
 * @Date:
-->
<template>
  <div>
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field v-model="username" name="username" label="用户名" placeholder="请输入用户名" />
        <van-field
          v-model="password"
          required
          type="password"
          name="password"
          label="密码"
          placeholder="请输入密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
      </van-cell-group>
      <div style="margin: 16px">
        <van-button block type="primary" native-type="submit"> 提交 </van-button>
      </div>
    </van-form>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import md5 from "js-md5";
import ACrypto from "@/utils/crypto";
import { showToast } from "vant";
import "vant/es/toast/style";
import { useRouter } from "vue-router";
import { useUserStore } from "@/stores/modules/user";
import useCurrentInstance from "@/utils/useCurrentInstance";

const userStore = useUserStore();
const router = useRouter();
const { proxy } = useCurrentInstance();
// 用户名
const username = ref("");
// 密码
const password = ref("");

const onSubmit = async (values: any) => {
  console.log("submit", values);
  // 1.执行登录接口
  const data = await proxy.$vueAxios.post(
    "MasterBaseUsers/Validate",
    {
      u_Name: ACrypto.setSS(username.value),
      u_PWD: md5(password.value)
    },
    { noLoading: true }
  );
  console.log(data);
  if (!data.Mark) {
    showToast({
      message: data.Msg,
      position: "top"
    });
  } else {
    // 存储token
    userStore.setToken(data.Model.U_Token);
    // 保存角色信息
    sessionStorage.setItem("userInfo", JSON.stringify(data.Model));
    const query1 = JSON.parse(sessionStorage.getItem("list-query") as any);
    switch (sessionStorage.getItem("pageNum")) {
      case "1": // 目录数据列表
        router.push({
          path: "/catalog",
          query: {
            fmid: query1.fmid,
            fmtid: query1.fmtid,
            catalogid: query1.catalogid,
            childrenFmtId: query1.childrenFmtId,
            status: query1.status,
            pageIndex: 1,
            pageSize: 20,
            searchType: 1,
            fondsId: query1.fondsId
          }
        });
        break;
      case "2": // 单条数据列表
        router.push({
          path: "/detail"
        });
        break;
    }
  }
};
</script>

<style scoped></style>
